﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Kenly.DBFramework.Gateway
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class EntityValidator : IEntityValidator
    {

        #region IEntityValidator 成员

        /// <summary>
        /// 
        /// </summary>
        public virtual bool IsValid
        {
            get
            {
                List<RuleViolation> violations;
                this.Validate(out violations);

                return (violations == null) ||
                    (violations.Count == 0);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="violations"></param>
        /// <returns></returns>
        public virtual bool Validate(out List<RuleViolation> violations)
        {
            violations = new List<RuleViolation>();
            return violations.Count == 0;
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static List<IEntityValidator> FindInvalid(IList entities)
        {
            List<IEntityValidator> invalidEntities = new List<IEntityValidator>();
            if (entities == null || entities.Count <= 0)
            {
                return invalidEntities;
            }

            for (int i = 0; i < entities.Count; i++)
            {
                if (entities[i] == null)
                {
                    continue;
                }

                IEntityValidator entityValidator = entities[i] as IEntityValidator;
                if (entityValidator != null && !entityValidator.IsValid)
                {
                    invalidEntities.Add(entityValidator);
                }
            }

            return invalidEntities;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IList<int> FindInvalidIndexes(IList entities)
        {
            List<int> invalidIndexes = new List<int>();

            if (entities == null || entities.Count <= 0)
            {
                return invalidIndexes;
            }

            for (int i = 0; i < entities.Count; i++)
            {
                if (entities[i] == null)
                {
                    invalidIndexes.Add(i);
                    continue;
                }

                IEntityValidator entityValidator = entities[i] as IEntityValidator;
                if (entityValidator != null && !entityValidator.IsValid)
                {
                    invalidIndexes.Add(i);
                }
            }

            return invalidIndexes;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entities"></param>
        public static void FilterInvalid(IList entities)
        {
            if (entities == null || entities.Count <= 0)
            {
                return;
            }

            for (int i = entities.Count - 1; i >= 0; i--)
            {
                if (entities[i] == null)
                {
                    entities.RemoveAt(i);
                    continue;
                }

                IEntityValidator entityValidator = entities[i] as IEntityValidator;
                if (entityValidator != null && !entityValidator.IsValid)
                {
                    entities.RemoveAt(i);
                }
            }
        }
    }
}
